library(texreg)
library(lme4)
library(lmerTest)
library(interplot)
library(sjPlot)
library(tidyverse)
library(sjstats)
library(plm)
library(clubSandwich)
library(marginaleffects)
library(Hmisc)
library(gtsummary)

#Load data
load("coalitionhistory_EJPR_RR.RData")

dyadic_parlgov_CSES_new <- dyadic_parlgov_CSES_new %>% 
  mutate(party_age = ifelse(party_age<0, 0, party_age))

dyadic_parlgov_familiarity_count <- dyadic_parlgov_CSES_new %>% 
  subset(!is.na(per_dist))%>%
  subset(select = c(country_name_short, dyad_id, start_date, cabinet_id, both_coalition,
                    prev_coalition_20yr, coalition_decay_95_60, coalition_decay_9_60, 
                    coalition_decay_99_60, gov_status_category
  ))%>%
  unique()

#Figure 1
ggplot(dyadic_parlgov_familiarity_count, aes(x=coalition_decay_95_60)) +
  geom_histogram(color="black", fill = "grey", binwidth=.5) +
  labs(title="", x="Number of Previous Coalitions", y = "Count") +
  theme_classic()

dyadic_parlgov_familiarity_cab <- dyadic_parlgov_familiarity %>% 
  subset(!is.na(per_dist))%>%
  subset(select = c(country_name_short, start_date, cabinet_id, cabinetnumber
  ))%>%
  unique()

#Figure 2
ggplot(dyadic_parlgov_familiarity_cab, aes(x=cabinetnumber)) +
  geom_histogram(color="black", fill = "grey", binwidth=.5) +
  labs(title="", x="Number of Cabinet Partners", y = "Count") +
  theme_classic()


#Models
#Model 1
dmodel1 <- lmer(per_dist ~ coalition_decay_95_60 + coalition_decay_95_60_sq + rile_dist 
                + party_age + prime_minister  + seatshare
                + age  + education + both_coalition + exclude
                + both_coalition*coalition_decay_95_60
                + exclude*coalition_decay_95_60
                + (1 | party_dyad_cabinet_id) + (1|country_name_short), data = dyadic_parlgov_CSES_new)
summary(dmodel1)

summary(dmodel1)

performance::icc(dmodel1)

#Model 2
dmodel1_prevtotal_ex <- lmer(per_dist ~ coalition_decay_95_60 + coalition_decay_95_60_sq + rile_dist 
                             + party_age + prime_minister  + seatshare + sum_prev_exclude_dyad
                             + age  + education + both_coalition + exclude
                             + coalition_decay_95_60*sum_prev_exclude_dyad
                             + both_coalition*coalition_decay_95_60
                             + exclude*coalition_decay_95_60
                             + (1 | party_dyad_cabinet_id) + (1|country_name_short), 
                             data = dyadic_parlgov_CSES_new)
summary(dmodel1_prevtotal_ex)

performance::icc(dmodel1_prevtotal_ex)


#Plots
#Marginal Effects
#Figure 3
interplot(m = dmodel1, var1 = "both_coalition", var2 = "coalition_decay_95_60") +
  xlab("Number of Previous Coalitions") +
  ylab("Marginal Effect of Current Coalition") +
  # Change the background
  theme_bw() +
  # Add the title
  # ggtitle("Marginal Effects of Both in Current Coalition on Perceived Distance") +
  theme(plot.title = element_text(face="bold")) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed")

#Figure 4
plot_model(dmodel1, type = "pred", terms = c("coalition_decay_95_60", "both_coalition"), 
           title = "",
           axis.title = c("Previous Coalition(s)", "Perceived Distance"),
           axis.labels = NULL,
           legend.title = "Current Coalition") + 
  theme_sjplot() +
  geom_hline(yintercept = 0, linetype = "dashed")

#Figure 5
interplot(m = dmodel1, var1 = "exclude", var2 = "coalition_decay_95_60") +
  xlab("Number of Previous Coalitions") +
  ylab("Marginal Effect of Current Exclusion") +
  # Change the background
  theme_bw() +
  # Add the title
  # ggtitle("Marginal Effects of Current Exclusion on Perceived Distance") +
  theme(plot.title = element_text(face="bold")) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed")

#Figure 6
interplot(m = dmodel1_prevtotal_ex, var1 = "coalition_decay_95_60", var2 = "sum_prev_exclude_dyad") +
  xlab("Total Coalition History") +
  ylab("Marginal Effect of Previous Coalitions") +
  # Change the background
  theme_bw() +
  # Add the title
  # ggtitle("Marginal Effects of Both in Current Coalition on Perceived Distance") +
  theme(plot.title = element_text(face="bold")) +
  # Add a horizontal line at y = 0
  geom_hline(yintercept = 0, linetype = "dashed")

#Report Models
texreg(c(dmodel1, dmodel1_prevtotal_ex))
